﻿#region File Description
//-----------------------------------------------------------------------------
// ChaseCamera.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TerrainGenerator
{
    abstract class Camera
    {
        public float fov = MathHelper.ToRadians(45);
        public float aspectRatio = 4.0f / 3.0f;
        public float nearPlane = 0.1f;
        public float farPlane = 1000.0f;
        abstract public Matrix GetViewMatrix();
        public Matrix GetProjectionMatrix()
        {
            return Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
        }
    }

    #region AimCam
    class AimCam
    {
        const float rotationSpeed = 0.01f;
        Vector3 positionOffset = new Vector3(0.0f, 20.0f, 70.0f);
        Vector3 lookatOffset = new Vector3(0.0f, 10.0f, 0.0f);

        public Vector3 position;
        public Vector3 camPosition;
        public Vector3 lookatPosition;
        float angle;

        public void SetTarget(Vector3 target)
        {
            // calc angle from position to target
            Vector3 direction = target - position;
            angle = (float)(Math.Atan2(direction.Z, direction.X)) + MathHelper.PiOver2;

            Console.WriteLine("set angle to: " + angle);
        }

        public Matrix GetViewMatrix()
        {
            Matrix transform = Matrix.Identity;
            transform.Up = Vector3.Up;
            transform.Right = Vector3.Cross(Vector3.Up, Vector3.UnitZ);
            transform *= Matrix.CreateRotationY(-angle);

            camPosition = position + Vector3.TransformNormal(positionOffset, transform);
            lookatPosition = position + Vector3.TransformNormal(lookatOffset, transform);

            return Matrix.CreateLookAt(camPosition, lookatPosition, Vector3.Up);
        }

        public void Update(GamePadState state)
        {
            // TODO
        }

#if !XBOX
        public void Update(KeyboardState state)
        {
            if (state.IsKeyDown(Keys.Left))
                angle += rotationSpeed;
            if (state.IsKeyDown(Keys.Right))
                angle -= rotationSpeed;

            Console.WriteLine("new angle: " + angle);
        }
#endif
    }
    #endregion
}
